<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 G1 垃圾回收器</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 16px;
            font-family: 'Consolas', 'Monaco', monospace;
            overflow-x: auto;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% - 12px);
            background: #e2e8f0;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    <i class="fas fa-memory mr-4"></i>G1 垃圾回收器
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    深入理解 JVM 中的 Garbage First 回收机制
                </p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-clock mr-2"></i>
                        <span>低停顿时间</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-chart-line mr-2"></i>
                        <span>高回收效率</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-database mr-2"></i>
                        <span>大堆内存支持</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                    <p class="text-lg leading-relaxed text-gray-700 indent-8">
                        在 JVM 中，G1（Garbage First）垃圾回收器是为了处理大堆内存应用而设计的，它试图在保证回收效率的同时，减少垃圾回收的停顿时间。G1 是 Java 7u4 引入的，并在后续版本中得到了改进。
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- G1 Goals -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">
                    G1 垃圾回收器的目标
                </h2>
                <div class="grid md:grid-cols-3 gap-8">
                    <div class="content-card rounded-xl shadow-lg p-8 text-center">
                        <div class="feature-icon text-5xl mb-4">
                            <i class="fas fa-stopwatch"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">低停顿时间</h3>
                        <p class="text-gray-600">G1 的主要目标是将垃圾回收的停顿时间控制在用户设定的目标范围内。</p>
                    </div>
                    <div class="content-card rounded-xl shadow-lg p-8 text-center">
                        <div class="feature-icon text-5xl mb-4">
                            <i class="fas fa-rocket"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">高回收效率</h3>
                        <p class="text-gray-600">通过优先回收垃圾最多的区域，提高垃圾回收的效率。</p>
                    </div>
                    <div class="content-card rounded-xl shadow-lg p-8 text-center">
                        <div class="feature-icon text-5xl mb-4">
                            <i class="fas fa-server"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">大堆内存支持</h3>
                        <p class="text-gray-600">适合大堆内存的应用场景，能够处理 TB 级别的内存。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- G1 Process Flow -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">
                    G1 垃圾回收流程
                </h2>
                
                <!-- Heap Structure -->
                <div class="content-card rounded-xl shadow-lg p-8 mb-8">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-cubes mr-3 text-purple-600"></i>堆内存结构
                    </h3>
                    <p class="text-gray-700 leading-relaxed indent-8">
                        G1 将堆内存划分为多个大小相等的区域（Region）。这些区域可以是年轻代（Young Generation）或老年代（Old Generation）。年轻代区域进一步划分为 Eden 区和两个 Survivor 区。
                    </p>
                    
                    <!-- Mermaid Diagram -->
                    <div class="mermaid mt-8">
                        graph LR
                            A[堆内存] --> B[Region 1]
                            A --> C[Region 2]
                            A --> D[Region 3]
                            A --> E[...]
                            B --> F[Eden]
                            B --> G[Survivor]
                            C --> H[Old]
                            D --> I[Old]
                            
                            style A fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
                            style F fill:#48bb78,stroke:#38a169,stroke-width:2px,color:#fff
                            style G fill:#4299e1,stroke:#3182ce,stroke-width:2px,color:#fff
                            style H fill:#ed8936,stroke:#dd6b20,stroke-width:2px,color:#fff
                            style I fill:#ed8936,stroke:#dd6b20,stroke-width:2px,color:#fff
                    </div>
                </div>

                <!-- GC Phases -->
                <div class="content-card rounded-xl shadow-lg p-8">
                    <h3 class="text-2xl font-bold mb-6 text-gray-800">
                        <i class="fas fa-sync-alt mr-3 text-purple-600"></i>垃圾回收阶段
                    </h3>
                    
                    <div class="space-y-8">
                        <div class="timeline-item">
                            <h4 class="text-xl font-bold mb-2 text-gray-800">1. 初始标记（Initial Mark）</h4>
                            <div class="bg-purple-50 rounded-lg p-4">
                                <p class="text-gray-700 mb-2"><strong>目的：</strong>标记从 GC Root 直接可达的对象。</p>
                                <p class="text-gray-700"><strong>特点：</strong>这个阶段需要暂停所有的应用线程（Stop The World, STW）。它通常是一个较短的暂停阶段。</p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <h4 class="text-xl font-bold mb-2 text-gray-800">2. 并发标记（Concurrent Mark）</h4>
                            <div class="bg-blue-50 rounded-lg p-4">
                                <p class="text-gray-700 mb-2"><strong>目的：</strong>在应用线程继续运行的同时，扫描整个堆，标记所有从初始标记阶段开始可达的对象。</p>
                                <p class="text-gray-700"><strong>特点：</strong>这个阶段与应用程序线程并发进行，尽量减少停顿时间。</p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <h4 class="text-xl font-bold mb-2 text-gray-800">3. 最终标记（Final Mark）</h4>
                            <div class="bg-green-50 rounded-lg p-4">
                                <p class="text-gray-700 mb-2"><strong>目的：</strong>完成标记的最后一步，处理在并发标记阶段可能遗漏的部分。</p>
                                <p class="text-gray-700"><strong>特点：</strong>这个阶段也需要暂停所有的应用线程（STW）。通常，它会在整个堆内存中对对象进行最终的标记。</p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <h4 class="text-xl font-bold mb-2 text-gray-800">4. 筛选和清理（Cleanup）</h4>
                            <div class="bg-yellow-50 rounded-lg p-4">
                                <p class="text-gray-700 mb-2"><strong>目的：</strong>选择需要回收的区域，并进行实际的垃圾回收操作。</p>
                                <p class="text-gray-700"><strong>特点：</strong>G1 会根据回收的目标时间，优先回收垃圾最多的区域。在这个阶段，G1 还会根据回收的目标时间决定是否需要暂停应用线程（STW）来完成垃圾回收。</p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Incremental GC -->
                <div class="content-card rounded-xl shadow-lg p-8 mt-8">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-layer-group mr-3 text-purple-600"></i>增量式垃圾回收
                    </h3>
                    <p class="text-gray-700 leading-relaxed">
                        <strong>增量式收集：</strong>G1 采用增量式的方式来回收内存，以减小垃圾回收的停顿时间。通过分阶段进行标记和回收，G1 可以逐步收集垃圾，避免一次性大规模的停顿。
                    </p>
                </div>
            </div>
        